package com.example.pceb.service.Impl;

import com.example.pceb.entity.StartupSocialInsuranceSubsidy;
import com.example.pceb.mapper.StartupSocialInsuranceSubsidyMapper;
import com.example.pceb.service.StartupSocialInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
public class StartupSocialInsuranceServiceImpl implements StartupSocialInsuranceService {

    @Autowired
    private StartupSocialInsuranceSubsidyMapper mapper;

    @Override
    @Transactional
    public void saveForm(StartupSocialInsuranceSubsidy form) {
        form.setStatus("未提交");
        form.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        if (form.getId() == null) {
            form.setCreateTime(new Timestamp(System.currentTimeMillis()));
            mapper.insert(form);
        } else {
            mapper.updateById(form);
        }
    }

    @Override
    @Transactional
    public void submitForm(StartupSocialInsuranceSubsidy form) {
        form.setStatus("审核中");
        form.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        if (form.getId() == null) {
            form.setCreateTime(new Timestamp(System.currentTimeMillis()));
            mapper.insert(form);
        } else {
            mapper.updateById(form);
        }
    }

    @Override
    @Transactional
    public void deleteForm(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public List<StartupSocialInsuranceSubsidy> getFormsByUsername(String username) {
        return mapper.selectUnsubmittedByUsername(username);
    }

    @Override
    public List<StartupSocialInsuranceSubsidy> getAllSubmittedForms(String username) {
        return mapper.selectSubmittedByUsername(username);
    }

    @Override
    public StartupSocialInsuranceSubsidy getFormById(Integer id) {
        return mapper.selectById(id);
    }
}